﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Linq;

using Microsoft.Win32;

using Path = System.IO.Path;

namespace AddCollisionLayerToMap
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btnopenfiles_OnClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog =new OpenFileDialog();
            dialog.Multiselect = true;
            dialog.Filter ="Text files (*.tmx)|*.tmx|All files (*.*)|*.*";
           // (*.bmp, *.jpg)|*.bmp;*.jpg|所有文件(*.*)|*.*”
            if (dialog.ShowDialog()==true)
            {
                foreach (string name in dialog.FileNames)
                {
                    fileslist.Items.Add(Path.GetFullPath(name));
                }
            }
        }

        private void ChangeLayerFile()
        {
            foreach (var item in fileslist.Items)
            {
                string path = item.ToString();
                if (File.Exists(path))
                {
                    this.LoadXMLDoc(path);
                }
                else
                {
                    Console.WriteLine("can't find file");
                }
            }
            MessageBox.Show("Complete");
        }

        private void ChangeFiles(object sender, RoutedEventArgs e)
        {
            this.ChangeLayerFile();
        }

        private void LoadXMLDoc(string path)
        {
            XDocument doc = XDocument.Load(path);
            XElement map = doc.Root;
            int width = Convert.ToInt32(map.Attribute("width").Value);
            int height = Convert.ToInt32(map.Attribute("height").Value);
            int tilewidth = Convert.ToInt32(map.Attribute("tilewidth").Value);
            int tileheight = Convert.ToInt32(map.Attribute("tileheight").Value);
            var test = doc.Elements("objectg111");
            var objectgroup = map.Element("objectgroup");
            bool change = false;
            if (objectgroup!=null)
            {
                int[] layer4 = new int[width * height];
                var objects = objectgroup.Elements("object");
                foreach (XElement element in objects)
                {
                    int posx = Convert.ToInt32(element.Attribute("x").Value);
                    int posy = Convert.ToInt32(element.Attribute("y").Value);
                    int column = posx / tilewidth;
                    int row = posy / tileheight;
                    var propertities = element.Element("properties");
                    if (propertities!=null)
                    {
                        var pros = propertities.Elements("property");
                        foreach (XElement pro in pros)
                        {
                            if (pro.Attribute("name").Value == "tile_id" && Convert.ToInt32(pro.Attribute("value").Value)!=0)
                            {
                                change = true;
                                int id = Convert.ToInt32(
                                    pro.Attribute("value").Value);
                                int gid = id - 384 + 1;
                                layer4[row * width + column] = gid;
                                Console.WriteLine("x:"+row+"y:"+column+"gid:"+gid);
                            }
                        }
                    }
                    //  int tileid = Convert.ToInt32(element.Attribute("x"));
                }

                if (change)
                {
                    //Find layers numbers.
                    var layers = map.Elements("layer");
                    int num = 1;
                    foreach (XElement layer in layers)
                    {
                        num += 1;
                    }
                    var addlayername = "Layer " + num;
                    string base64 = Base64Tool.IntArrayToBase64(layer4);
                    XElement data = new XElement("data", new XAttribute("encoding", "base64"));
                    data.SetValue(base64);
                    XElement addElement = new XElement("layer", new XAttribute("name",addlayername),new XAttribute("width",width),new XAttribute("height",height));
                    addElement.Add(data);
                    map.Add(addElement);
                    doc.Save(path);
                   
                }
            }
            
        }
    }
}
